﻿#include "TcpServer.h"
#include "MyTcpServer.h"

TcpServer::TcpServer(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    loadConfig();
    // listen if there has client want to connect
    MyTcpServer::getInstance().listen(QHostAddress(m_strIP), m_usPort);
}

TcpServer::~TcpServer()
{}

void TcpServer::loadConfig()
{
    QFile file(":/server.config");
    if (file.open(QIODevice::ReadOnly)) {
        QByteArray baData = file.readAll();
        QString strData = baData.toStdString().c_str();
        //qDebug() << strData;
        file.close();

        strData.replace("\r\n", " ");
        //qDebug() << strData;
        QStringList strList = strData.split(" ");
        //for (int i = 0; i < strList.size(); i++) {
            //qDebug() << "---" << strList[i];
        //}
        m_strIP = strList.at(0);
        m_usPort = strList.at(1).toUShort();
        qDebug() << "ip:" << m_strIP << " port:" << m_usPort << endl;
    }
    else {
        QMessageBox::critical(this, "open config", "open config failed");
    }
}

/***************************************************/
void TcpServer::recoverDB() {
    OpeDB::getInstance().handleRecover();
    return;
}
